<?php

namespace app\common\model\shop;

use think\Model;
use traits\model\SoftDelete;

/**
 * sku商品插件
 */
class SkuGoods extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'jeek_shop_sku_goods';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'price',
        'discount_price',
        'vip_price',
        'images_array',
        'image_str',
        'total_stock',
    ];
    public function getSalesAttr($value, $data)
    {
        return bcadd($value, $data['virtual_sales'], 0);
    }
    public function getImageStrAttr($value, $data)
    {
        return $data['image'] ? cdnurl($data['image'], true) : $data['image'];
    }
    public function getImagesArrayAttr($value, $data)
    {
        if ($data['images']) {
            $array = explode(',', $data['images']);
            foreach ($array as &$v) {
                $v = cdnurl($v, true);
            }
            return $array;
        }
        return [];
    }
    // 获取商品价格区间
    public function getPriceAttr($v, $d)
    {
        $specProduct = new SkuProduct();
        $list = $specProduct->where('goods_id', $d['id'])->order('price', 'asc')->column('price');
        // $len = count($list);
        return $list[0];
        // if($len == 1) return $list[0];
        // return $list[0] == $list[$len-1]?$list[0]:$list[0].'~'.$list[$len-1];
    }
    // 获取商品一折价格区间
    public function getDiscountPriceAttr($v, $d)
    {
        $specProduct = new SkuProduct();
        $list = $specProduct->where('goods_id', $d['id'])->order('price', 'asc')->column('price');
        return bcmul($list[0], 0.1, 2);
    }
    // 获取商品Vip价格
    public function getVipPriceAttr($v, $d)
    {
        $specProduct = new SkuProduct();
        $list = $specProduct->where('goods_id', $d['id'])->order('price', 'asc')->column('vip_price');
        return $list[0];
    }
    // 获取商品总库存
    public function getTotalStockAttr($v, $d)
    {
        $specProduct = new SkuProduct();
        return $specProduct->where('goods_id', $d['id'])->order('price', 'asc')->sum('stock');
    }
    /**
     * 一对一关联分类
     *
     * @return void
     */
    public function category()
    {
        return $this->belongsTo('app\common\model\goods\Category', 'category_id')->setEagerlyType(0);
    }

    /**
     * 获取vue参数
     *
     * @param [type] $ids
     * @return void
     */
    static function getVueInitData($ids)
    {
        // 规格
        $groups = [];
        $items  = [];
        $specs  = (new SkuSpec())->with('skuItems')->where('goods_id', $ids)->select();
        $arr    = collection($specs)->toArray();
        $itemKey = []; //商品使用
        foreach ($specs as $k => $v) {
            $groups[$k]['title'] = $v['name'];
            $i = [];
            foreach ($v['sku_items'] as $kk => $vv) {
                $i[$kk]['name'] = $vv['name'];
                $i[$kk]['id'] = $vv['id'];
                $itemKey[$vv['id']] = $vv['name'];
            }
            $groups[$k]['items'] = $i;
        }
        // 商品
        $products = (new SkuProduct())->where('goods_id', $ids)->select();
        $products = collection($products)->toArray();
        foreach ($products as $k => $v) {
            $keyArr = explode('_', $v['key']);
            $attr = [];
            foreach ($keyArr as $kk => $vv) {
                $attr[] = $itemKey[$vv];
            }
            // unset($products[$k]['id']);
            // $products[$k]['attr'] = $attr;
        }
        return ['groups' => $groups, 'products' => $products];
    }
}
